/*
 * @Date: 2021-11-16 13:47:39
 * @Version: 0.0.1
 * @Author: chenhengling
 * @Description: prompt
 * @LastEditTime: 2022-01-23 14:17:39
 * @LastEditors: Please set LastEditors
 */
const inquirer = require('inquirer');
const { templates } = require('./config');
const getGitConfig = require('./gitConfig');

const { user } = getGitConfig('global') || {};
const defaultAuthor = user ? `${user.name ?? ''}${user.email ? ' <' + user.email + '>' : ''}` : '';

const questions = [
  {
    type: 'list',
    name: 'type',
    message: '请选择模板类型',
    choices: templates.map((v) => ({ name: `${v.name}(${v.desc})`, value: v.type })),
  },
  {
    name: 'name',
    message: '请输入项目名称',
    validate(input) {
      if (!input) {
        return '项目名称不能为空';
      }
      return true;
    },
  },
  {
    name: 'description',
    message: '请输入项目描述',
  },
  {
    name: 'author',
    message: '请输入作者',
    default: defaultAuthor,
  },
];

function prompt() {
  return inquirer.prompt(questions).catch((error) => {
    if (error.isTtyError) {
      console.error("Prompt couldn't be rendered in the current environment");
    } else {
      console.error(error);
    }
    throw error;
  });
}

module.exports = { prompt };

if (require.main === module) {
  prompt().then((answers) => {
    console.log(answers);
  });
}
